let App = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    member: {},
    shows: true,
    infoShows: true,
    option: {},
    pageNumber: 1,
    scrollHeight: null,
    curNav: 2,
    list: [],
    productCategories: [
      {
        "name": "收藏",
        "id": "2"
      },
      {
        "name": "点赞",
        "id": "1"
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {

    var that = this;
    // console.log('xx:'+wx.getStorageSync('user_id'));

    if (wx.getStorageSync('token') != '') {
      that.setData({
        shows: false,
        infoShows: true
      })
    } else {
      that.setData({
        shows: true,
        infoShows: false
      })
    }
    
    // 记录option
    that.setData({ option }, function () {
      // 获取商品列表
      that.getProductList(true);
    });
    
  },
  onShow(){
    this.getUserDetail();
    this.getTabBar().setData({
      selected:2
    })
    const productList = this.selectComponent('#productList');
    if (productList) {
      // 调用子组件方法
      productList.getProductList(true);
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  /**
   * 获取商品列表
   */
  getProductList: function (is_super, pageNumber) {
    let _this = this;
    App._get('/api/product/list', {
      pageNumber: pageNumber || 1,
      orderType: 'PRICE_DESC',
      productCategoryId: 0
    }, function (result) {
      let resultList = result.data
        , dataList = _this.data.list;
      if (is_super === true || typeof dataList === 'undefined') {
        _this.setData({ list: resultList, noList: false });
      } else {
        _this.setData({ 'list.productItems': dataList.productItems.concat(resultList.productItems) });
      }

    });
  },

  /**
 * 下拉到底加载数据
 */
  bindDownLoad: function () {
    // 已经是最后一页
    if (this.data.pageNumber >= this.data.lastPage) {
      this.setData({ no_more: true });
      return false;
    }
    this.getProductList(false, ++this.data.pageNumber);
  },

  /**
   * 设置商品列表高度
   */
  setListHeight: function () {
    let _this = this;
    wx.getSystemInfo({
      success: function (res) {
        _this.setData({
          scrollHeight: res.windowHeight - 90,
        });
      }
    });
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

    let _this = this;
    var sh = _this.data.shows;
    _this.setData({
      shows: false
    })

  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
 * 获取当前用户信息
 */
  getUserDetail: function () {
    let _this = this;
    App._get('/mapi/user/index', {}, function (result) {
      _this.setData({ member: result.data });
    });
  },

  /**
   * 订单导航跳转
   */
  onTargetOrder(e) {
    // 记录formid
    let urls = {
      all: '/pages/order/list?status=',
      payment: '/pages/order/list?status=PENDING_PAYMENT',
      received: '/pages/order/list?status=SHIPPED',
      shipped: '/pages/order/list?status=PENDING_SHIPMENT',
      pending: '/pages/order/list?status=PENDING_REVIEW'
    };
    // 转跳指定的页面
    wx.navigateTo({
      url: urls[e.currentTarget.dataset.type]
    })
  },

  /**
  * 菜单列表导航跳转
  */
  onTargetMenus(e) {
    // 记录formId
    // App.saveFormId(e.detail.formId);
    wx.navigateTo({
      url: '/' + e.currentTarget.dataset.url
    })
  },
  goUserDetail(e) {
    this.getUserDetail();
  },


  goNav() {
    console.log(wx.getStorageSync('user_id'));
    if (wx.getStorageSync('user_id') == '') {
      let url = '../../pages/login/login';
      wx.navigateTo({
        url: url,
      })
    }
  },
  gotoNav() {
    const userinfo = { user_nickname: this.data.member.user_nickname, phone: this.data.member.phone, avatar: this.data.member.avatar }
    wx.navigateTo({
      url: '../personage/personalInfo?userInfo=' + JSON.stringify(userinfo)
    })
  },
  selectNav: function (t) {
    let curNav = t.target.dataset.id;
    this.setData({
      curNav,
      scrollTop: 0
    });
    const productList = this.selectComponent('#productList');
    if (productList) {
      // 调用子组件方法
      productList.getProductList(true);
    }
  },
  getPhoneNumber: function (e) {
    const _this = this
    const code = e.detail.code;
    wx.login({
      success: res => {
        if (res.code) {
          wx.getUserInfo({
            success: res1 => {
              console.log(res, '---')
              wx.request({
                url: 'https://sucaiku.it-zx.com/mapi/login/getUserPhoneNumber',
                method: 'POST',
                data: {
                  phonenumber_code: code,
                },
                success: res2 => {
                  App._post_form('/mapi/login/wxLogin'
                    , {
                      js_code: res.code,
                      // rawData: e.detail.rawData,
                      user_nickname: res1.userInfo.nickName,
                      avatar_url: res1.userInfo.avatarUrl,
                      phonenumber: res2.data.data.phonenumber
                      // encryptedData: e.detail.encryptedData,
                      // ivStr: e.detail.iv,
                      // signature: e.detail.signature,
                      // distinctId:getApp().sensors.store.getDistinctId()
                    }
                    , function (result) {
                      console.log(result);
                      // 记录token user_id
                      wx.setStorageSync('token', result.data.token);
                      wx.setStorageSync('user_id', result.data.id);
                      wx.setStorageSync('open_id', result.data.open_id);
                      _this.setData({'shows':false})
                      // 跳转回原页面
                      // sa.login(result.data.id);
                    }
                    , false
                    , function () {
                      wx.hideLoading();
                      _this.getUserDetail()
                      
                    });
                }
              });
            }
          });
        }
      }
    });
  },
   refreshCurrentPage() {
    // 获取当前页面栈
    const pages = getCurrentPages();
    // 获取当前页面的实例
    const currentPage = pages[pages.length - 1];
  
    // 刷新页面数据
    currentPage.refreshPageData(); // 假设当前页面有refreshPageData方法
  
    // 返回当前页面
    wx.navigateBack({
      delta: 1
    });
  },

})